﻿using System.Windows.Controls;

namespace KlearTweet.Client.DataContainers
{
    public partial class DataWrapPanel
    {
        public Panel Last
        {
            get
            {
                if (PanelsContainer.Children.Count == 0)
                    AddNewLine();
                return (Panel)PanelsContainer.Children[PanelsContainer.Children.Count - 1];
            }
        }

        public DataWrapPanel()
        {
            InitializeComponent();
        }

        public void Clear()
        {
            PanelsContainer.Children.Clear();
        }

        public void Add(string text)
        {
            // Break into words to support text wrapping
            var words = text.Split(' ');
            for (int i = 0; i < words.Length; i++)
            {
                AddTextBlock(words[i]);
                if (i + 1 < words.Length)
                    AddTextBlock(" ");
            }
        }

        private void AddTextBlock(string text)
        {
            if (!string.IsNullOrEmpty(text))
                Last.Children.Add(new TextBlock { Text = text, FontSize = 12 });
        }

        public void Add(System.Windows.UIElement obj)
        {
            Last.Children.Add(obj);
        }

        public void AddNewLine()
        {
            if (PanelsContainer.Children.Count > 0 && Last.Children.Count ==0)
                Add(" "); // To make sure that this empty line takes the vertical space that it must take
            PanelsContainer.Children.Add(new WrapPanel());
        }

        public void ClipboardCopyAll()
        {
            var text = new System.Text.StringBuilder();
            for (int i = 0; i < PanelsContainer.Children.Count; i++)
            {
                var panel = (Panel) PanelsContainer.Children[i];
                foreach (var child in panel.Children)
                {
                    if (child is TextBlock)
                        text.Append(((TextBlock) child).Text);
                    else if (child is TextBox)
                        text.Append(((TextBox) child).Text);
                    else if (child is ContentControl && ((ContentControl) child).Content is string)
                        text.Append(((ContentControl) child).Content);
                }
                if (i + 1 < PanelsContainer.Children.Count) // Another panel/line is coming
                    text.AppendLine();
            }

            Infrastructure.ThreadPoolHelper.RunOnMainThread(() => // TODO: Move to an Infrastructure class!
            {
                // Copy to clipboard
                var clipboardData = (System.Windows.Browser.ScriptObject)
                    System.Windows.Browser.HtmlPage.Window.GetProperty("clipboardData");
                if (clipboardData != null)
                {
                    var success = (bool)clipboardData.Invoke("setData", "text", text.ToString());
                    if (!success)
                        InfoController.Show("TODO: User refused copy of:\n" + text); // TODO: Provide an alternative
                }
                else
                    InfoController.Show("TODO: Browser doesn't support copy of:\n" + text); // TODO: Provide an alternative
            });
        }
    }
}
